package com.oilsection.demo.service.impl;

import com.oilsection.demo.dao.*;
import com.oilsection.demo.po.*;
import com.oilsection.demo.entity.specJzInfo;
import com.oilsection.demo.service.JzListService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author yangyuge
 * @description
 * @Date 2024/5/3 16:12
 */
@Service
public class JzListServiceImpl implements JzListService {
    private static final Logger logger = LoggerFactory.getLogger(JzListServiceImpl.class);
    @Autowired
    private GetJzListSourceDao getJzListSourceDao;
    @Autowired
    private GetQkListSourceDao getQkListSourceDao;
    @Autowired
    private GetXldListSourceDao getXldListSourceDao;

    @Autowired
    private GetCkListSourceDao getCkListRepository;

    @Autowired
    private GetHkListSourceDao getHkListSourceDao;

    @Autowired
    private GetJkxlListSourceDao getJkxlListSourceDao;

    @Override
    public specJzInfo getSpecJzInfo(String id) {
        specJzInfo res = new specJzInfo();
        JzInfoTable jzInfoTable = new JzInfoTable();
        try{
            jzInfoTable = getJzListSourceDao.findById(id).get();
        }catch(Exception e){}
        res.setJzState(jzInfoTable.getJzState());
        DecimalFormat df = new DecimalFormat("#.##");
        String xPoint = df.format(Double.parseDouble(jzInfoTable.getJkXpoint())) + "°";
        String yPoint = df.format(Double.parseDouble(jzInfoTable.getJkYpoint())) + "°";
        res.setJzPoint(xPoint + "," + yPoint);
        res.setJzId(jzInfoTable.getId());
        res.setJcdNum(jzInfoTable.getJcdNum());
        res.setJzName(jzInfoTable.getJzName());
        List<Map<String, String>> kqxlList = new ArrayList<>();
        List<Map<String, String>> cqxlList = new ArrayList<>();
        List<Map<String, String>> hkxlList = new ArrayList<>();
        List<Map<String, String>> eventList = new ArrayList<>();
        List<Map<String,String>> jkxlList = new ArrayList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy年MM月dd日");
        List<XldInfoTable> xldInfoTables = new ArrayList<>();
        try{
            xldInfoTables = getXldListSourceDao.findByJzIdAndDealSection(id, "未处理");
        }catch(Exception e) { }
        for (XldInfoTable ele : xldInfoTables) {
            Map<String, String> mapKqxl = new HashMap<>();
            mapKqxl.put("xldId", ele.getId());
            String xldXpoint = df.format(Double.parseDouble(ele.getXldcXpoint())) + "°";
            String xldYpoint = df.format(Double.parseDouble(ele.getXldcYpoint())) + "°";
            mapKqxl.put("xldPoint", "(" + xldXpoint + "," + xldYpoint + ")");
            String d = sdf1.format(ele.getFindTime());
            mapKqxl.put("findTime", d);
            kqxlList.add(mapKqxl);
        }
        //历史事件
        List<XldInfoTable> xldInfoTables1 = new ArrayList<>();
        try {
            xldInfoTables1 = getXldListSourceDao.findByJzId(id);
        }catch(Exception e){ }
        for (XldInfoTable ele : xldInfoTables1) {
            Map<String, String> mapEvent = new HashMap<>();
            mapEvent.put("eventType", "泄露");
            String d = sdf1.format(ele.getFindTime());
            mapEvent.put("updateTime", d);
            eventList.add(mapEvent);
        }
        //串库历史事件
        List<CkInfoTable> ckInfoTables = new ArrayList<>();
        try {
            ckInfoTables = getCkListRepository.findByCkyIdOrSrIdAndDealSection(id,id,"未处理");
        }catch(Exception e){}
        for (CkInfoTable ele : ckInfoTables) {
            Map<String, String> mapEvent = new HashMap<>();
            String d = sdf.format(ele.getFindTime());
            String d1 = sdf1.format(ele.getFindTime());
            mapEvent.put("eventType", "串库");
            mapEvent.put("updateTime", d1);
            eventList.add(mapEvent);
        }
        //未处理串库
        List<CkInfoTable> ckInfoTables1 = new ArrayList<>();
        try{
            ckInfoTables1 = getCkListRepository.findByCkyIdOrSrIdAndDealSection(id,id,"未处理");
        }catch (Exception e){}

        for (CkInfoTable ele : ckInfoTables1) {
            Map<String, String> mapCqxl = new HashMap<>();
            mapCqxl.put("ckId", ele.getId());
            String ckType = "";
            String reJz = "";
            if (id.equals(ele.getCkyId())) {
                ckType = "本井组渗出";
                reJz = ele.getSrId();
            } else if (id.equals(ele.getSrId())) {
                ckType = "外井组渗入";
                reJz = ele.getCkyId();
            }
            mapCqxl.put("ckType", ckType);
            mapCqxl.put("jzId", reJz);
            String d = sdf1.format(ele.getFindTime());
            mapCqxl.put("findTime",d);
            cqxlList.add(mapCqxl);
        }
        List<HkInfoTable> hkInfoTables = new ArrayList<>();
        try {
            hkInfoTables = getHkListSourceDao.findByJzId(id);
        }catch(Exception e){}
        for (HkInfoTable ele : hkInfoTables) {
            Map<String, String> mapEvent = new HashMap<>();
            String d1 = sdf1.format(ele.getFindTime());
            mapEvent.put("eventType", "环空泄露");
            mapEvent.put("updateTime", d1);
            eventList.add(mapEvent);
        }
        //未处理环空泄露
        List<HkInfoTable> hkInfoTables1 = new ArrayList<>();
        try{
            hkInfoTables1 = getHkListSourceDao.findByJzIdAndDealSection(id,"未处理");
        }catch (Exception e){}
        for (HkInfoTable ele : hkInfoTables1){
            Map<String, String> mapHkxl = new HashMap<>();
            mapHkxl.put("hkId", ele.getId());
            String hkXpoint = df.format(Double.parseDouble(ele.getJzXpoint())) + "°";
            String hkYpoint = df.format(Double.parseDouble(ele.getJzYpoint())) + "°";
            mapHkxl.put("jkPoint", "(" + hkXpoint + "," + hkYpoint + ")");
            String d = sdf1.format(ele.getFindTime());
            mapHkxl.put("findTime", d);
            hkxlList.add(mapHkxl);
        }
        //井口泄露历史问题
        List<JkxlInfoTable> jkxlInfoTable1 = new ArrayList<>();
        try{
            jkxlInfoTable1 = getJkxlListSourceDao.findByXljzId(id);
        }catch (Exception e){}
        for (JkxlInfoTable ele:jkxlInfoTable1){
            Map<String, String> mapEvent = new HashMap<>();
            String d1 = sdf1.format(ele.getFindTime());
            mapEvent.put("eventType", "井口泄露");
            mapEvent.put("updateTime", d1);
            eventList.add(mapEvent);
        }
        List<JkxlInfoTable> jkxlInfoTables = new ArrayList<>();
        try{
            jkxlInfoTables = getJkxlListSourceDao.findByXljzIdAndDealSection(id,"未处理");
        }catch(Exception e){}

        for (JkxlInfoTable ele:jkxlInfoTables){
            Map<String,String> mapJk = new HashMap<>();
            mapJk.put("jkId",ele.getId());
            String jkXpoint = df.format(Double.parseDouble(jzInfoTable.getJkXpoint()))+"°";
            String jkYpoint = df.format(Double.parseDouble(jzInfoTable.getJkYpoint()))+"°";
            mapJk.put("jkPoint","(" + jkXpoint + "," + jkYpoint + ")");
            String d = sdf1.format(ele.getFindTime());
            mapJk.put("findTime", d);
            jkxlList.add(mapJk);
        }
        res.setKqxlNum(kqxlList.size());
        res.setCqxlNum(cqxlList.size());
        res.setHkxlNum(hkxlList.size());
        res.setJkxlNum(jkxlList.size());
        res.setKqxlList(kqxlList);
        res.setCqxlList(cqxlList);
        res.setHkxlList(hkxlList);
        res.setJkxlList(jkxlList);
        res.setEventList(eventList);
        return res;
    }

    @Override
    public List<String> getJzNameList(String kqName) {
        List<JzInfoTable> list;
        if(kqName.equals(""))
        {
            list = getJzListSourceDao.findAll();
        }
        else{
            List<KqInfoTable> Kqlist=getQkListSourceDao.findByName(kqName);
            String kqId="";
            if(Kqlist.size()>=1)
            {
                kqId=Kqlist.get(0).getId();
            }
            list = getJzListSourceDao.findByKqId(kqId);
        }
        List<String> jzNameList = new ArrayList<>();
        for(JzInfoTable ele:list){
            jzNameList.add(ele.getJzName());
        }
        return jzNameList;
    }
}
